<?php

class AdminProductCustomerController extends AdminController {

	public function getCreate($id)
	{
		$model = new ProductCustomer;
		$products = $model->productCustomers($id);
		$selected_products = $model->selectedProducts($id);
		$data['products'] = $products;

		$customer = Customer::find($id);
		$data['customer'] = $customer;

		$data['brand'] = Taxonomy::whereType('brand')->lists('taxonomy', 'id');

		$this->layout->title = trans('customer.create_customer');
		$this->layout->content = View::make('admin.product_customer.form', $data);
	}

	public function postCreate()
	{
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax())
		{
			$data  = Input::get('product');
			$customer = Input::get('customer', 0);
			$result = array('error' => true, 'message' => 'Something is error!');

			if($customer > 0 && count($data) > 0)
			{
				
			}
		}

		return json_encode($result);
	}

	public function postAdd()
	{
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax())
		{
			$result = array('error' => true, 'message' => 'Something is error!');

			$data['customer']  = Input::get('customer', 0);
			$data['product']  = Input::get('product', 0);

			if($data['customer'] > 0 && $data['product'] > 0)
			{
				$data['price'] = 0;

				$model = new ProductCustomer;
				$model->saveData($data);

				$result = array('error' => false, 'message' => 'Data is saved');
			}
		}

		return json_encode($result);
	}

	public function postAddByBrand()
	{
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax())
		{
			$result = array('error' => true, 'message' => 'Something is error!');

			$data['customer']  = Input::get('customer', 0);
			$brand = Input::get('brand', 0);

			if($data['customer'] > 0 && $brand > 0)
			{
				$products = Product::whereBrand($brand)->lists('id');
				
				foreach ($products as $product) {
					$data['price']   = 0;
					$data['product'] = $product;

					$model = new ProductCustomer;
					$checkExists = ProductCustomer::whereCustomer($data['customer'])
									->whereProduct($product)
									->count();

					if($checkExists == 0){
						$rs = $model->saveData($data);
					}
				}

				$result = array('error' => false, 'message' => 'data saved');
			}
		}

		return json_encode($result);
	}

	public function postAddAll()
	{
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax())
		{
			$result = array('error' => true, 'message' => 'Something is error!');

			$data['customer']  = Input::get('customer', 0);

			if($data['customer'] > 0)
			{
				$products = Product::lists('id');
				
				foreach ($products as $product) {
					$data['price']   = 0;
					$data['product'] = $product;

					$model = new ProductCustomer;
					$checkExists = ProductCustomer::whereCustomer($data['customer'])
									->whereProduct($product)
									->count();

					if($checkExists == 0){
						$rs = $model->saveData($data);
					}
				}

				$result = array('error' => false, 'message' => 'data saved');
			}
		}

		return json_encode($result);
	}

	public function postChangePrice(){
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax())
		{
			$result = array('error' => true, 'message' => 'Something is error!');
			
			$id     = Input::get('id', 0);
			$price  = Input::get('price', 0);

			$model            = new ProductCustomer;
			$product_customer = $model->findOrFail($id);
			$validator        = $model->validator(array('price' => $price));

			if($product_customer !== false && $validator->fails() === false)
			{
				$product_customer->price = $price;
				$product_customer->save();

				$result = array('error' => false, 'message' => 'Data is saved');
			}else{
				$result = array('error' => true, 'message' => $model->getValidatorMessage());
			}
		}

		return json_encode($result);
	}

	public function postGet(){
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax())
		{
			$result = array('error' => true, 'message' => 'Something is error!');

			$customer  = Input::get('customer', 0);

			$model = new ProductCustomer;
			$data = $model->selectedProducts($customer);

			if(count($data) > 0){
				$result = array('error' => false, 'data' => $data);
			}
		}

		return json_encode($result);
	}

	public function postDelete()
	{
		$error   = true;
		$message = "Only accept ajax request!";

		if(Request::ajax()){
			$id    = Input::get('id', 0);
			
			$model = new ProductCustomer;
			$product_customer = $model->findOrFail($id);

			$error   = true;
			$message = "product customer not found!";

			if($product_customer !== false){
				$product_customer->delete();

				$error   = false;
				$message = "data is deleted";
			}
		}

		return json_encode(array('error' => $error, 'message' => array('message' => $message)));
	}

	public function postDeleteAll()
	{
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax()){
			$customer = Input::get('customer', false);
			
			if($customer != false){
				ProductCustomer::whereCustomer($customer)->delete();

				$result = array('error' => false, 'message' => 'deleted');
			}
		}

		return json_encode($result);
	}

	public function postDeleteByBrand()
	{
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax()){
			$customer = Input::get('customer', false);
			$brand    = Input::get('brand', false);
			
			if($customer != false && $brand != false){
				$products = Product::whereBrand($brand)->lists('id');

				ProductCustomer::whereCustomer($customer)->whereIn('product', $products)->delete();

				$result = array('error' => false, 'message' => 'deleted');
			}
		}

		return json_encode($result);
	}
}